﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Services;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.Inventory.BL;

namespace com.vs.WebService
{
    /// <summary>
    /// Summary description for InventoryService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class InventoryService : System.Web.Services.WebService
    {
        string XSLT_DIR = "xslt";

        [WebMethod]
        public string GetItems(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = ItemDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "ITEM";
                dt.DataSet.DataSetName = "ITEMS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return XMLDocManager.TransformStringWithFile(sbXML.ToString(), Server.MapPath(XSLT_DIR) + "\\ItemLookUp.xslt", Encoding.UTF8);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetItem(string stSessID, string stCompany, string stItem)
        {
            Dictionary<string, KeyValuePair<string, object>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new Dictionary<string, KeyValuePair<string, object>>();

                if (StringManager.IsEmpty(stCompany) ||
                    StringManager.IsEmpty(stItem))
                    return null;
                else
                {
                    dcCond.Add("ITEM", new KeyValuePair<string, object>("=",stItem));
                    dcCond.Add("COMPANY",new KeyValuePair<string, object>("=",stCompany));
                }


                dt = ItemDO.GetTableByColumns(sess, DictionaryManager.ToList(dcCond), null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "ITEM";
                dt.DataSet.DataSetName = "ITEMS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return XMLDocManager.TransformStringWithFile(sbXML.ToString(), Server.MapPath(XSLT_DIR) + "\\ItemLookUp.xslt", Encoding.UTF8);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetItemUMsASC(string stSessID, string stCompany, string stItem)
        {
            Session sess = null;
            Dictionary<string, KeyValuePair<string, object>> dcCond = null;
            DataTable dt = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new Dictionary<string, KeyValuePair<string, object>>();
                dcCond.Add("COMPANY", new KeyValuePair<string, object>("=", stCompany));
                dcCond.Add("ITEM", new KeyValuePair<string, object>("=", stItem));
                dt = ItemUnitOfMeasureDO.GetTableByColumns(sess, DictionaryManager.ToList(dcCond), " ORDER BY SEQUENCE ASC");
                if (DataManager.IsEmpty(dt))
                    return MessageManager.MSG_ITM_001; //未找到货品单位设定

                dt.TableName = "UM";
                dt.DataSet.DataSetName = "UMS";
                sbXML = new StringBuilder();
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                return XMLDocManager.TransformStringWithFile(sbXML.ToString(), Server.MapPath(XSLT_DIR) + "\\ItemUM.xslt", Encoding.UTF8);

            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetBaseUM(string stSessID, string stCompany, string stItem)
        {
            Session sess =null;
            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                return ItemManager.GetBaseUM(sess, stItem, stCompany);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetBaseQty(string stSessID, string stCompany, string stItem, string stQty, string stQtyUM)
        {
            Session sess = null;
            int iQty = 0;

            try
            {
                iQty = Int32.Parse(stQty);
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                return "" + ItemManager.GetBaseQty(sess, stItem, stCompany, iQty, stQtyUM);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetInventories(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            InventoryManager mgr = null;
            String stErr = null;

            try
            {
                sess = (Session) Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;
                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                //dt = LocationInventoryDO.GetTableByColumns(sess, dcCond, " ORDER BY ITEM, LOCATION");
                mgr = new InventoryManager();
                dt = mgr.GetInventoryByQFCri(sess, dcCond, " ORDER BY LI.ITEM, LI.LOCATION", ref stErr);
                if (!StringManager.IsEmpty(stErr))
                    return stErr;
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "LOCATION_INVENTORY";
                dt.DataSet.DataSetName = "LOCATION_INVENTORIES";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();

            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string TransferInventory(string stSessID, string stXML)
        {
            Session sess = null;
            String stTransXML = null;
            InventoryDS fromInvDS, toInvDS = null;
            InventoryHelper invMgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                stTransXML = XMLDocManager.TransformStringWithFile(stXML, Server.MapPath(XSLT_DIR) + "\\FromInventoryDS.xslt", Encoding.UTF8);
                fromInvDS = (InventoryDS)XMLDocManager.Deserialize(typeof(InventoryDS), XMLDocManager._XML_HEADER + stTransXML);
                stTransXML = XMLDocManager.TransformStringWithFile(stXML, Server.MapPath(XSLT_DIR) + "\\ToInventoryDS.xslt", Encoding.UTF8);
                toInvDS = (InventoryDS)XMLDocManager.Deserialize(typeof(InventoryDS), XMLDocManager._XML_HEADER + stTransXML);

                invMgr = new InventoryHelper();
                return invMgr.TransferInventory(sess, fromInvDS, toInvDS, false);
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + ": " + e.Message;
            }
        }

        [WebMethod]
        public string AdjustInventory(string stSessID, string stXML)
        {
            Session sess = null;
            //String stAdjustXML = null;
            InventoryDS invDS = null;
            InventoryHelper invMgr = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                //stAdjustXML = XMLDocManager.TransformStringWithFile(stXML, XSLT_DIR + "InventoryDS.xslt", Encoding.UTF8);
                invDS = (InventoryDS)XMLDocManager.Deserialize(typeof(InventoryDS), XMLDocManager._XML_HEADER + stXML);

                if (invDS.LPN == "")
                    invDS.LPN = null;
                if (invDS.PARENT_LPN == "")
                    invDS.PARENT_LPN = null;
                if (invDS.ATTRIBUTE1 == "")
                    invDS.ATTRIBUTE1 = null;
                if (invDS.ATTRIBUTE2 == "")
                    invDS.ATTRIBUTE2 = null;
                if (invDS.ATTRIBUTE3 == "")
                    invDS.ATTRIBUTE3 = null;
                if (invDS.ATTRIBUTE4 == "")
                    invDS.ATTRIBUTE4 = null;
                if (invDS.ATTRIBUTE5 == "")
                    invDS.ATTRIBUTE5 = null;
                if (invDS.ATTRIBUTE6 == "")
                    invDS.ATTRIBUTE6 = null;
                if (invDS.ATTRIBUTE7 == "")
                    invDS.ATTRIBUTE7 = null;
                if (invDS.ATTRIBUTE8 == "")
                    invDS.ATTRIBUTE8 = null;


                invMgr = new InventoryHelper();
                return invMgr.AdjustInventory(sess, invDS, "+", "", "");
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetAttributes(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;
                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = AttributeDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "TRANSACTION_HISTORY";
                dt.DataSet.DataSetName = "TRANSACTION_HISTORIES";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();

            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string SaveAttribute(string stSessID, string stXML)
        {
            AttributeDO attrDO = null;
            Session sess = null;
            StringBuilder sbXML = new StringBuilder();

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                attrDO = (AttributeDO)XMLDocManager.Deserialize(typeof(AttributeDO), XMLDocManager._XML_HEADER + stXML);
                if (attrDO.ATTRIBUTE_NUM > 0)
                {
                    attrDO.Update(sess);
                    return null;
                }
                else
                {
                    attrDO.Insert(sess);
                    return null;
                }
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetLocations(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();

                if (!StringManager.IsEmpty(stCondXML))
                {
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);
                }


                dt = LocationDO.GetTableByColumns(sess, dcCond, null);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "LOCATIONS";
                dt.DataSet.DataSetName = "LOCATION";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();

            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string RemoveAllNegativeInv(string stSessID)
        {
            Session sess = null;
            InventoryManager invMgr = null;

            sess = (Session)Application.Get(stSessID);
            if (null == sess)
                return MessageManager.MSG_SES_001;
            invMgr = new InventoryManager();
            return invMgr.DeleteAllNegativeInv(sess);
        }
    }
}
